﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Groupware;

using Stats;
using Storage;
using Security;

namespace GUI
{
    public partial class userProfile : TripLOA.GUI.internationalization.LOABasePage
    {
           
        TripLoaUser us;
        //Security.Profile usProfileOwner;
        Security.Profile usProfile;
       
        string guest;
        protected void Page_Load(object sender, EventArgs e)
        {
            /*Look other profile*/
            guest = Request.QueryString["Nick"];
            
            us = TripLoaUserManager.Instance.getUser(User.Identity.Name);
                       
            // for ban user guest
           // usProfileOwner = us.GetProfile();

            if (guest == null || guest == us.UserName)
            {
                SendMess.Visible = false;
                btn_ban_user.Visible = false;
                btn_add_friend.Visible = false;
                usProfile = us.GetProfile();
                Stats.Objects.UserProfile us_stats = Groupware.UserManager.getUserStats(us.UserID);
                               
                // For personal stats
                Label_Proposed_Trips_Number.Text = us_stats.UserStats.ProposedTripsNumber.ToString();
                Label_Total_Km.Text = us_stats.UserStats.TotalKm.ToString();
                Label_Money_Saved.Text = us_stats.UserStats.TotalMoneySaved.ToString();
                Label_Total_Pollution_Saved.Text = us_stats.UserStats.TotalPollutionSaved.ToString();
                Label_Fuel_Saved.Text = us_stats.UserStats.TotalFuelSaved.ToString();
                Label_Driver_Trips_Number.Text = us_stats.UserStats.AsDriverTripsNumber.ToString();
                Label_Passenger_Trips_Number.Text = us_stats.UserStats.AsPassengerTripsNumber.ToString();
                Label_Max_Passenger_Carried.Text = us_stats.UserStats.MaxPassengersCarried.ToString();
                Label_Total_Passnger_Carried.Text = us_stats.UserStats.TotalPassengersCarried.ToString();

            }
            // Guest's profile
            else
            {
                btn_friends.Visible = false;
                btn_change_profile.Visible = false;
                user_stat.Visible = false;
                //TO REMOVE WHEN TE PROTOCOL FOR BAN AND UNBAN A USER IS READY
                //btn_ban_user.Visible = false; 
                //------------------------------------------------------------
                usProfile = us.GetProfile(guest);
                if (!us.GetProfile().IsFriendOf(guest))
                {
                    btn_add_friend.Visible = true;
                }

            }

            if(usProfile.isVisible("Username"))
                Label_UserNick.Text = usProfile.Username;
                

            if (usProfile.Gender.ToString().Equals("Male"))
            {
                Label_Male.Visible = true;
                Label_Female.Visible = false;
            }
            else if (usProfile.Gender.ToString().Equals("Female"))
            {
                Label_Male.Visible = false;
                Label_Female.Visible = true;
            }
            else
            {
                Label_Male.Visible = false;
                Label_Female.Visible = false;
                Label_GenderT.Visible = false;
            }


            if (usProfile.Smoker == Storage.EntityModels.Smoker.Smoker)
            {
                Label_Smoker_Yes.Visible = true;
                Label_Smoker_No.Visible = false;
                Label_Smoker_Unknown.Visible = false;
            }
            else if (usProfile.Smoker == Storage.EntityModels.Smoker.NotSmoker)
            {
                Label_Smoker_Yes.Visible = false;
                Label_Smoker_Unknown.Visible = false;
                Label_Smoker_No.Visible = true;
            }
            else
            {
                Label_Smoker_Yes.Visible = false;
                Label_Smoker_Unknown.Visible = true;
                Label_Smoker_No.Visible = false;
            }
            
            // Get the Avatar
            string av = usProfile.AvatarPath;
          
            // Check if the avatar image exist 
            if (!File.Exists(av))
                img_avatar.ImageUrl = usProfile.GetDefaultAvatar();
            else
                img_avatar.ImageUrl = av;
            

            // For rating star
            Rating_Feedbacks.CurrentRating = (int)usProfile.GeneralFeedback.Vote;
            int temp = (int)usProfile.DriverFeedback.FeedbacksAsDriverCount + (int)usProfile.PassengerFeedback.FeedbacksAsPassengerCount;
            Label_FeedbacksNumber.Text = "(" + temp.ToString() + ")";
            Rating_DriverFeedbacks.CurrentRating = (int)usProfile.DriverFeedback.Vote;
            Label_DriverFeedbacksNumber.Text = "(" + usProfile.DriverFeedback.FeedbacksAsDriverCount.ToString() + ")";
            Rating_PassengersFeedbacks.CurrentRating = (int)usProfile.PassengerFeedback.Vote;
            Label_PassengerFeedbacksNumber.Text = "(" + usProfile.PassengerFeedback.FeedbacksAsPassengerCount.ToString() + ")";

                       
            if(usProfile.Age != -1)
                Label_Age.Text = usProfile.Age.ToString();
           

            // For user Profile
            if (usProfile.Birthday.HasValue)
                Label_Birthday.Text = usProfile.Birthday.Value.ToShortDateString().ToString();

            Label_City.Text = usProfile.City;
            Label_Address.Text = usProfile.Address;
            Label_Name.Text = usProfile.Name;
            Label_Surname.Text = usProfile.Surname;
            Label_Email.Text = usProfile.Email;

            Label_PhoneNumber.Text = usProfile.PhoneNumber;
            List_BuddyList.Visible = true;
            List_BuddyList.DataSource = usProfile.BuddyList;

            List_SocialNetworks.Visible = true;
            List_SocialNetworks.DataSource = usProfile.SocialNetworks;

            List_Interest.Visible = true;
            List_Interest.DataSource = usProfile.Interests;

            List_Feedbacks.Visible = true;
            List_Feedbacks.DataSource = usProfile.Feedbacks;
            
            /*
            List_Vehicles.Visible = true;
            List_Vehicles.DataSource = userProfile.VehiclesDetails;
             */

            //int vehicle = Groupware.UserManager.getUserVehicleType(us.UserID);
            int vehicle = us.GetUserVehicleType(usProfile);
            const int error = -1;

            switch (vehicle)
            {
                case (int) Stats.StatsModule.vehicleTypes.UtilitaryCarVehicleType:      
                // UTILITARY CAR TYPE SELECTED
                        {
                            Label_Utilitary.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.SedanCarVehicleType:      
                // SEDAN CAR VEHICLE SELECTED
                        {
                            Label_Sedan.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.SportCarVehicleType:      
                // SPORT CAR VEHICLE SELECTED
                        {
                            Label_Sport.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.SUVCarVehicleType:      
                // SUV CAR VECHICLE SELECTED
                        {
                            Label_Suv.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.MPVCarVehicleType:     
                // Multi Purpose Vehicle SELECTED
                        {
                            Label_Multipurpose.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.MotorbikeVehicleType:      
                // MOTORBIKE VEHICLE SELECTED
                        {
                            Label_Motorbike.Visible = true;
                            break;
                        }

                case (int) Stats.StatsModule.vehicleTypes.NoOwnVehicle:
                // THE USER NOT HAVE A VEHICLE
                        {
                            Label_No_Vehicles.Visible = true;
                            break;
                        }

                case error:
                        {
                            break;
                        }
                

                default:
                    {
                        /*
                        Groupware.UserManager.setUserVehicleType(us.UserID, (int) StatsModule.vehicleTypes.UtilitaryCarVehicleType);
                        goto case (int) Stats.StatsModule.vehicleTypes.UtilitaryCarVehicleType;
                         */
                        break;
                    }
            }
            
            /*
            string av = usProfile.AvatarPath;
            avatar.Style.Add("background", "url(./img/comments_footer.png) no-repeat;");
            avatar.Style.Add("min-height", "100px");
            */

            if (usProfile.Feedbacks.Count >= 5)
            {
                inpDown.Visible = true;
                inpUp.Visible = true;
                DropDownList_Comment.Visible = true;
                Label_Speed.Visible = true;
            }

            if (DropDownList_Comment.Attributes["onchange"] == null)
            {
                DropDownList_Comment.Attributes.Add("onchange", "javascript:set_speed();");
            }
            
            // For Feedbacks details+
            try
            {
                if (usProfile.GeneralFeedback.Punctuality.ToString().Length > 4)
                    Label_General_Punctuality.Text = usProfile.GeneralFeedback.Punctuality.ToString().Remove(4) + " / 10";
                else
                    Label_General_Punctuality.Text = usProfile.GeneralFeedback.Punctuality.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_General_Punctuality.Text = "6";
            }
            try
            {
                if (usProfile.GeneralFeedback.Reliability.ToString().Length > 4)
                    Label_General_Reliability.Text = usProfile.GeneralFeedback.Reliability.ToString().Remove(4) + " / 10";
                else
                    Label_General_Reliability.Text = usProfile.GeneralFeedback.Reliability.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_General_Reliability.Text = "6";
            }
            try
            {

                if (usProfile.DriverFeedback.Punctuality.ToString().Length > 4)
                    Label_Driver_Punctuality.Text = usProfile.DriverFeedback.Punctuality.ToString().Remove(4) + " / 10";
                else
                    Label_Driver_Punctuality.Text = usProfile.DriverFeedback.Punctuality.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_Driver_Punctuality.Text = "6";
            }
            try
            {
                if (usProfile.DriverFeedback.Reliability.ToString().Length > 4)
                    Label_Driver_Reliability.Text = usProfile.DriverFeedback.Reliability.ToString().Remove(4) + " / 10";
                else
                    Label_Driver_Reliability.Text = usProfile.DriverFeedback.Reliability.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_Driver_Reliability.Text = "6";
            }
            try
            {
                if (usProfile.DriverFeedback.Courtesy.ToString().Length > 4)
                    Label_Driver_Kindness.Text = usProfile.DriverFeedback.Courtesy.ToString().Remove(4) + " / 10";
                else
                    Label_Driver_Kindness.Text = usProfile.DriverFeedback.Courtesy.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_Driver_Kindness.Text = "6";
            }
            try
            {
                if (usProfile.PassengerFeedback.Punctuality.ToString().Length > 4)
                    Label_Passenger_Punctuality.Text = usProfile.PassengerFeedback.Punctuality.ToString().Remove(4) + " / 10";
                else
                    Label_Passenger_Punctuality.Text = usProfile.PassengerFeedback.Punctuality.ToString() + " / 10";
            }
            catch (System.ArgumentOutOfRangeException)
            {
                //Default value
                Label_Passenger_Punctuality.Text = "6";
            }
            try
            {
                if (usProfile.PassengerFeedback.Reliability.ToString().Length > 4)
                    Label_Passenger_Reliability.Text = usProfile.PassengerFeedback.Reliability.ToString().Remove(4) + " / 10";
                else
                    Label_Passenger_Reliability.Text = usProfile.PassengerFeedback.Reliability.ToString() + " / 10";
            }
            catch(System.ArgumentOutOfRangeException)
            {
                //Default 
                Label_Passenger_Reliability.Text = "6";
            }

            
        }
        
        protected void Page_PreRender(object sender, EventArgs e)
        {
            List_Feedbacks.DataBind();
            List_BuddyList.DataBind();
            List_Interest.DataBind();
            List_SocialNetworks.DataBind();
            //List_Vehicles.DataBind();
        }

        protected void Btn_SendPM_Click(object sender, EventArgs e)
        {
    
            if (guest != null)
            {

                String textSubject = TextBox_Subject.Text;
                if (textSubject.Trim().Equals(""))
                    textSubject = "[click]";

                TextBoxTextMess.Text.Replace("\n", "\n\r");  
                
                bool priority = CheckBoxPriority2.Checked;
                Message sendMess = new Message { Title = textSubject, Text = TextBoxTextMess.Text, Priority = priority };
                sendMess.Send(us, guest);
                
            }
        }
        protected void ButtonClose_Click(object sender, EventArgs e)
        {
            SendMess.Visible = false;
        }

        protected void btn_ban_user_Click(object sender, EventArgs e)
        {
            us.BanUser(guest);
            
            // By Elia Contini : un utente bannato non va elimitato dalla lista degli amici
            // la RemoveFriend è simmetrica mentre il ban è asimmetrico.
            /*if (us.GetProfile().IsFriendOf(guest))
            {
                us.RemoveFriend(guest);
            }*/
           
        }

        protected void btn_add_friend_Click(object sender, EventArgs e)
        {
            //if (!us.GetProfile().IsFriendOf(guest))
            //{
            //    us.AddFriend(usProfile.Id);
            //}
            FriendshipRequest req = new FriendshipRequest();
            req.AskForFriendship(us, us.GetProfile(guest).Id);
        }

 
    }
}
